/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.esisar.picaso.ihm;

import fr.esisar.picaso.entites.Commande;
import fr.esisar.picaso.entites.Format;
import fr.esisar.picaso.entites.Livre;
import fr.esisar.picaso.requetes.RequetesCommande;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author THOMAS
 */
public class FenetreInterneCommande extends javax.swing.JInternalFrame {

	private static final long serialVersionUID = 1835127404514373372L;
	/**
     * Creates new form FenetreInterneCommande
     */
    private Commande commandes;
    private FenetrePrincipaleClient mere;
    
    public FenetreInterneCommande(Commande commandes, FenetrePrincipaleClient mere) {
        this.commandes = commandes;
        initComponents();
        initTable();
        this.mere = mere;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Nouvelle commande");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Commander");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        RequetesCommande r = new RequetesCommande();
        try {
            r.ajouterCommande(commandes, mere.client.getId());
        } catch (SQLException ex) {
            Logger.getLogger(FenetreInterneCommande.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    @SuppressWarnings("serial")
	private void initTable() {
        int n = commandes.getAlbumsCommandes().size();
        String[][] tableau = new String[n][7];
        for(int i = 0 ; i<n ; i++){
            Livre l = (Livre) commandes.getAlbumsCommandes().get(i);
            Format f =  commandes.getFormatsCommandes().get(i);
            tableau[i][0] = l.getTitre();
            tableau[i][1] = "" + l.getPhotos().size();
            tableau[i][2] = (l.getPostface()==null && l.getPreface()==null && l.getIdPhoto()==-1)?"Non":"Oui";
            tableau[i][3] = f.getType();
            tableau[i][4] = "" + f.getPrix();
            tableau[i][5] = "" + commandes.getQtes().get(i);
            tableau[i][6] = "" + commandes.getQtes().get(i)*f.getPrix();
        }
        jTable1.setModel(new javax.swing.table.DefaultTableModel(tableau, new String[] {"Titre", "Nombre photos", "Livre", "Format", "Prix unitaire", "Quantité", "Prix total"})
            {
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return false;
                }
            });
    }
}
